package com.ruoyi.api.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.api.annotation.Login;
import com.ruoyi.business.app.bo.AppMerchantOrderBo;
import com.ruoyi.business.app.bo.AppMerchantQueryBo;
import com.ruoyi.business.app.vo.*;
import com.ruoyi.business.domain.*;
import com.ruoyi.business.service.*;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;


/**
 * @description: 商家类相关接口
 * @author: kubilewang
 * @date: 2022/7/11 14:01
 **/

@Api(tags = "首页--->商家相关接口")
@AllArgsConstructor
@RestController
@RequestMapping("/api/merchant")
public class MerchantController extends BaseController {

    private final IHcxMerchantService iHcxMerchantService;

    private final IHcxOrderEvaluateService orderEvaluateService;

    private final IHcxOrderEvaluatePicsService orderEvaluatePicsService;

    private final IHcxMerchantProductService iHcxMerchantProductService;

    private final IHcxMerchantOrderService iHcxMerchantOrderService;

    private final IHcxMerchantPhotoService iHcxMerchantPhotoService;

    private final IHcxUserCouponService iHcxUserCouponService;

    private final RedisCache redisCache;

    /**
     * 查询商家列表
     */
    @ApiOperation("查询商家列表")
    @GetMapping("/list")
    public TableDataInfo<AppMerchantVo> list(AppMerchantQueryBo bo) {
        return iHcxMerchantService.queryAppPageList(bo);
    }

    @ApiOperation("商家详情")
    @GetMapping("/detail")
    public AjaxResult<AppMerchantDetailVo> detail(@ApiParam(value = "商家主键id", required = true) @NotBlank Long id) {
        AppMerchantDetailVo lwzgFacVo = iHcxMerchantService.queryAppById(id);
        return AjaxResult.success(lwzgFacVo);
    }


    @ApiOperation("查询某个商家的所有订单的评价数据")
    @GetMapping("/evaluate/list")
    public TableDataInfo<AppOrderEvaluateVo> evaluateList(AppMerchantOrderBo bo) {
        return orderEvaluateService.getOrderEvaluateList(bo);
    }


    @ApiOperation("某条评价的详细信息")
    @GetMapping("/evaluate/detail")
    public AjaxResult<AppOrderEvaluateVo> evaluateDetail(@ApiParam(value = "订单评价id", required = true) Long id) {
        AppOrderEvaluateVo appOrderEvaluateVo = orderEvaluateService.getVoById(id, AppOrderEvaluateVo.class);
        // 查询评价照片
        List<AppOrderEvaluatePicsVo> photosList = orderEvaluatePicsService.listVo(
                new LambdaQueryWrapper<HcxOrderEvaluatePics>()
                        .eq(HcxOrderEvaluatePics::getOrderEvaluateId, appOrderEvaluateVo.getId()), AppOrderEvaluatePicsVo.class);
        appOrderEvaluateVo.setPhotos(photosList);
        return AjaxResult.success(appOrderEvaluateVo);
    }


    @Login
    @ApiOperation("购买商家的某个产品")
    @RepeatSubmit
    @PostMapping("/purchase")
    public AjaxResult<AppPurchaseVo> purchase(HttpServletRequest request, @ApiParam(value = "商品id", required = true) @RequestParam Long productId,
                                     @ApiParam(value = "优惠券id") @RequestParam(required = false) Long couponId,
                                     @ApiParam(value = "支付方式(1 微信支付 2支付宝支付 3银联支付 4余额支付)", required = true) @RequestParam String payType
    ) throws Exception {
        AppPurchaseVo appPurchaseVo = iHcxMerchantProductService.purchase(request, productId, couponId, payType);
        return AjaxResult.success(appPurchaseVo);
    }


    @ApiOperation("查询商品详情")
    @GetMapping("/product/detail")
    public AjaxResult<AppMerchantProductDetailVo> productDetail(@ApiParam(value = "商品id", required = true) Long id) {

        AppMerchantProductDetailVo appMerchantProductDetailVo = iHcxMerchantProductService.getVoById(id, AppMerchantProductDetailVo.class);

        AppMerchantVo appMerchantVo = iHcxMerchantService.getVoById(appMerchantProductDetailVo.getMerchantId(), AppMerchantVo.class);

        // 查询商家订单数量
        int count = iHcxMerchantOrderService.count(new LambdaQueryWrapper<HcxMerchantOrder>()
                .eq(HcxMerchantOrder::getMerchantId, appMerchantProductDetailVo.getMerchantId())
                .gt(HcxMerchantOrder::getStatus,0)
                .lt(HcxMerchantOrder::getStatus,4)
        );
        appMerchantVo.setSaleNum(count);


        // 取第一张门面照
        List<HcxMerchantPhoto> list = iHcxMerchantPhotoService.list(new LambdaQueryWrapper<HcxMerchantPhoto>().eq(HcxMerchantPhoto::getMerchantId, appMerchantProductDetailVo.getMerchantId()));
        if (CollectionUtil.isNotEmpty(list)) {
            appMerchantVo.setPicUrl(list.get(0).getPicUrl());
        }

        appMerchantProductDetailVo.setAppMerchantVo(appMerchantVo);
        return AjaxResult.success(appMerchantProductDetailVo);
    }


    @Login
    @ApiOperation("购买商品时查询用户所拥有的优惠券")
    @GetMapping("/coupon")
    public AjaxResult<AppUserCouponVo> coupon(@ApiParam(value = "商品id", required = true) @RequestParam Long id) {

        AppUserCouponVo appUserCouponVo = new AppUserCouponVo();
        HcxMerchantProduct hcxMerchantProduct = iHcxMerchantProductService.getById(id);
        // 查询用户所拥有的符合条件的优惠券 筛选条件为 (1 未使用  2 未过期(当前时间在优惠券开始和结束时间之内) 3 订单金额满足优惠券的满减金额)
        // 假设商家价格103    用户有两张面额20、30的优惠券都满足要求   面额20(满100元可用)   面额30(满100元可用)  则优先使用这个30面额大的优惠券

        List<HcxUserCoupon> list = iHcxUserCouponService.list(new LambdaQueryWrapper<HcxUserCoupon>()
                .eq(HcxUserCoupon::getUserId, SecurityUtils.getUserId())
                .apply("curdate() >= begin_date")
                .apply("curdate() <= end_date ")
                .le(HcxUserCoupon::getConditionPrice, hcxMerchantProduct.getProPrice())
                .eq(HcxUserCoupon::getStatus, Constants.ZERO).orderByDesc(HcxUserCoupon::getDenomination));

        List<AppUserCouponVo> result = new ArrayList<>();
        for (HcxUserCoupon hcxUserCoupon : list) {
            AppUserCouponVo userCouponVo = new AppUserCouponVo();

            userCouponVo.setId(hcxUserCoupon.getId());
            userCouponVo.setDenomination(hcxUserCoupon.getDenomination());

            result.add(userCouponVo);
        }

        if (CollectionUtil.isNotEmpty(result)) {
            appUserCouponVo = result.get(0);
        }

        // 订单有效期
        String orderValid = redisCache.getCacheObject(Constants.SYS_CONFIG_KEY + Constants.HCXORDERVALID);
        appUserCouponVo.setOrderValid(orderValid);
        return AjaxResult.success(appUserCouponVo);
    }


}
